package com.gwctv.result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Result<T> 统一后端响应结果
 *
 * @param <T> 泛型
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Tag(name = "响应结果类", description = "统一项目响应结果")
public class Result<T> {
    @Schema(name = "code", description = "响应状态码(1成功，0和其它数字为失败)", type = "integer")
    private Integer code;

    @Schema(name = "msg", description = "响应信息", type = "string")
    private String msg;

    @Schema(name = "data", description = "响应结果", type = "T")
    private T data;

    @Operation(summary = "成功响应", description = "成功响应且无具体响应值")
    public static <T> Result<T> success() {
        return new Result<T>(1, "成功", null);
    }

    @Operation(summary = "成功响应", description = "成功响应且有具体响应值")
    public static <T> Result<T> success(T object) {
        return new Result(1, "成功", object);
    }

    @Operation(summary = "响应失败", description = "响应失败向前端返回失败原因")
    public static<T>  Result<T> error(String msg) {
        return new Result<T>(0, msg, null);
    }
}

